<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>
    <script src="../js/prop-types.js"></script>
</head>

<body>
    <div id="root"></div>
    <script type="text/babel">
        class Child extends React.Component {
            constructor(props) {
                super(props);
                // getInitState周期
                this.state = {
                    name: "world",
                    val: "fn",
                    date: new Date(),
                };
                this.changeState = this.changeState.bind(this);
                this.destroyComponent = this.destroyComponent.bind(this)
            }
            changeState() {
                this.setState({
                    name: "hello world"
                })
            }
            destroyComponent() {
                ReactDOM.unmountComponentAtNode(document.getElementById('root'));
            }
            tick() {
                this.setState({
                    date: new Date(),
                })
            }

            componentWillMount() {
                console.log('componentWillMount');
            }
            componentDidMount() {
                console.log('componentDidMount');
                this.clock = setInterval(() => this.tick(), 1000);
            }


            componentWillReceiveProps() {
                console.log('props changed');
                this.setState({
                    val: 'props has been changed'
                })
            }
            shouldComponentUpdate(nextProps, nextState) {
                return (
                    nextProps.val !== 'setState' &&
                    nextState.val !== 'props has been changed'
                )
            }
            componentWillUpdate() {
                console.log('componentWillUpdate');
            }
            componentDidUpdate() {
                console.log('componentDidUpdate');
            }
            componentWillUnmount() {
                console.log('destroy');
                clearInterval(this.clock);
            }

            render() {
                return (
                    <div>
                        <h1>name: {this.state.name}</h1>
                        <button onClick={this.changeState}>change state</button>
                        <h1>value: {this.props.val}</h1>
                        <h1>fn: {this.state.val}</h1>
                        <button onClick={this.props.fn}>
                            change props form parent
                        </button>
                        <h1>Date:{this.state.date.toLocaleTimeString()}</h1>
                        <button onClick={this.destroyComponent}>destroy component</button>
                    </div>
                )
            }
        }
        // getDefaultProps周期
        Child.defaultProps = {
            name: "hello",
        }
        Child.propTypes = {
            name: PropTypes.string,
        }

        class Parent extends React.Component {
            constructor(props) {
                super(props);
                this.state = {
                    val: "World",
                }
                this.changeState = this.changeState.bind(this);
            }
            changeState() {
                console.log('change props');
                this.setState({
                    val: "setState"
                })
            }
            render() {
                console.log('Parent render()');
                return (
                    <div>
                        <h1>in parent state.val = {this.state.val}</h1>
                        <Child val={this.state.val} fn={this.changeState} />
                    </div>
                )
            }
            shouldComponentUpdate(nextProps, nextState) {
                return nextState.val !== 'setState'
            }
        }

        ReactDOM.render(<Parent />, document.getElementById('root'))
    </script>
</body>

</html>

<!-- 
实例化：getDefaultProps - getInitialState - componentWillMount - render - componentDidMount
存在期：componentWillReceiveProps - shouldComponentUpdate - componentWillUpdate - componentDidUpdate
销毁：componentWillUnmount
 -->